package com.ihighest.app.infox.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.SelecColumnListAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

public class SubscriptionColumntwoActivity extends BaseActivity {

	private ActivityTitle title;
	SelecColumnListAdapter adapter;
	ArrayList<String> list;
	TextView emptyView;
	PullToRefreshListView pullToRefresh;

	int page = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_subscription);

		list = new ArrayList<String>();
		list = getIntent().getStringArrayListExtra(Const.STEPLIST);

		title = (ActivityTitle) findViewById(R.id.id_sub_title);
		title.setTitle("企业号");
		title.setViewVersible(title.titleBack, title.rightText);
		title.setViewGone(title.titleMsg, title.titleRefresh);
		title.setRightTextViewText("继续");

		title.rightText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = null;
				if (list.size() != 0) {

					switch (list.get(0)) {
					case "tag":
						intent = new Intent(SubscriptionColumntwoActivity.this, LableBindingActivity.class);
						break;
					case "columnist":
						intent = new Intent(SubscriptionColumntwoActivity.this, SubscriptionColumnActivity.class);
						break;
					case "enterprise":
						intent = new Intent(SubscriptionColumntwoActivity.this, SubscriptionColumntwoActivity.class);
						break;
					}
					if (list.size() >= 1) {
						// 如果步骤很多，将步骤数据传给第一个界面
						list.remove(0);
						intent.putStringArrayListExtra(Const.STEPLIST, list);
					} else {
						intent = new Intent(SubscriptionColumntwoActivity.this, MainActivity.class);
					}
					startActivity(intent);
				} else {
					intent = new Intent(SubscriptionColumntwoActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				}
			}
		});

		inint();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void inint() {

		emptyView = (TextView) findViewById(R.id.emptyView);
		emptyView.setText("加载中...");
		emptyView.setVisibility(View.VISIBLE);

		adapter = new SelecColumnListAdapter(this);

		pullToRefresh = (PullToRefreshListView) findViewById(R.id.pullToRefresh);
		pullToRefresh.setVisibility(View.GONE);
		pullToRefresh.setAdapter(adapter);
		pullToRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				page = 1;
				getdata();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				getdata();
			}
		});

		getdata();
	}

	private void getdata() {
		Api.get("channel/subscribes", ApiParam.create().addParam("cid", "cid").addParam("page", page).addParam("type", "recommendE"),
				new ApiCallback(SubscriptionColumntwoActivity.this, true) {

					@Override
					public void handleSuccess(ApiResult result) {
						if (result.getCode() == 100) {
							if (result.getDatas() != null && result.getDatas().length() > 0) {
								emptyView.setVisibility(View.GONE);
								pullToRefresh.setVisibility(View.VISIBLE);
								if (page == 1) {
									adapter.setDatas(result.getDatas());
								} else {
									adapter.appendDatas(result.getDatas());
								}
							} else if (page == 1) {
								emptyView.setText("暂无数据");
								emptyView.setVisibility(View.VISIBLE);
								pullToRefresh.setVisibility(View.GONE);
							} else {
								page--;
							}
						} else if (page == 1) {
							emptyView.setText("暂无数据");
							emptyView.setVisibility(View.VISIBLE);
							pullToRefresh.setVisibility(View.GONE);
						}
						pullToRefresh.onRefreshComplete();
					}

					@Override
					public void handleFailure(Exception e, String msg) {
						if (page == 1) {
							emptyView.setText("暂无数据");
							emptyView.setVisibility(View.VISIBLE);
							pullToRefresh.setVisibility(View.GONE);
						} else {
							page--;
						}
						pullToRefresh.onRefreshComplete();
					}
				}, SubscriptionColumntwoActivity.this);

	}
}